package com.zry.simpleBlog.controller;


import com.zry.simpleBlog.comment.aop.annotations.AuthCheck;
import com.zry.simpleBlog.comment.aop.annotations.Idempotent;
import com.zry.simpleBlog.comment.enums.RoleEnum;
import com.zry.simpleBlog.comment.enums.IdempotentStrategyEnum;
import com.zry.simpleBlog.comment.respBean.RespBean;
import com.zry.simpleBlog.dto.CommentDTO;
import com.zry.simpleBlog.service.ICommentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 */
@RestController
@Tag(name = "评论管理接口")
public class CommentController {
    @Resource
    private ICommentService commentService;


    @GetMapping("comments/{blogId}")
    public RespBean comments(@Parameter(name = "pageNum", description = "页码") @RequestParam(defaultValue = "-1") Integer pageNum,
                             @Parameter(name = "pageSize", description = "页大小") @RequestParam(defaultValue = "-1") Integer pageSize,
                             @PathVariable String blogId) {
        return commentService.pageCommentByBlogId(pageNum, pageSize, blogId);
    }

    @Operation(summary = "发表评论", description = "实现幂等性校验")
    @Idempotent(timeout = 5, strategy = IdempotentStrategyEnum.IDEMPOTENT_INTERFACE)
    @AuthCheck(isLogin = false)
    @PostMapping("comments")
    public RespBean post(@RequestBody @Valid CommentDTO comment) {
        return commentService.saveComment(comment);
    }

    @Operation(summary = "删除评论")
    @AuthCheck(rank = RoleEnum.GOD)
    @DeleteMapping("comments/{id}")
    public RespBean delete(@PathVariable String id) {
        return commentService.removeComment(id);
    }
}
